package com.odm.communitymanage.ui.help

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.SeekHelpData
import com.odm.communitymanage.ui.ImageDetailActivity
import com.odm.communitymanage.util.GlideUtils
import kotlinx.android.synthetic.main.fragment_help_detail.*

/**
 * @description: 求助具体详情页面
 * @author: ODM
 * @date: 2020/4/14
 */
class HelpDetailFragment :BaseMVVMFragment(){



    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        registerLiveEvent()
    }

    override fun initViewModelObserve() {

    }

    override fun initViews() {

    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_help_detail
    }

    private fun registerLiveEvent(){
        LiveEventBus.get(Constants.EVENT_HELP_DETAIL , SeekHelpData::class.java)
            .observeSticky(this  , object : Observer<SeekHelpData> {
                override fun onChanged(data : SeekHelpData) {
                    loadHelpDetail(data)
                }
            })
    }

    private fun loadHelpDetail(data : SeekHelpData ?){
        val contactInfo :String  = data?.username + " "+data?.phone
        tvContactInfo_HelpDetail.text  = contactInfo
        tvDescription_HelpDetail.text = data?.content
        tvRemark_HelpDetail.text = data?.remarks
        tvTime_HelpDetail.text = data?.time
        val imgUrls = data?.imgs ?: mutableListOf()
        //图片加载
        for(url in imgUrls){
            var target : ImageView ?= null
            if (ivPhoto_1_HelpDetail.visibility != View.VISIBLE) {
                target = ivPhoto_1_HelpDetail
            } else if (ivPhoto_2_HelpDetail.visibility != View.VISIBLE) {
                target = ivPhoto_2_HelpDetail
            } else if (ivPhoto_3_HelpDetail.visibility != View.VISIBLE) {
                target = ivPhoto_3_HelpDetail
            }
            target?.apply {
                visibility = View.VISIBLE
                //加载图片
                GlideUtils
                    .getInstance()
                    .loadImageUrl(requireContext(),url ,target)
                //点击事件，跳转加载大图Activity
                setOnClickListener {
                    LiveEventBus
                        .get(Constants.IMAGE_URL)
                        .post(url)
                    ActivityUtils.startActivity(ImageDetailActivity::class.java)
                }
            }
        }
        if(imgUrls.isEmpty()){
            //无图片情况下，保底在第一张图片的位置显示一张 默认图
            GlideUtils.getInstance()
                .loadImageResourceId(requireContext(), R.mipmap.ic_launcher ,ivPhoto_1_HelpDetail )
        }
        //加载 求助的状态
        when(data?.status){
            0-> {
                tvState_HelpDetail.setTextColor(resources.getColor(R.color.help_unsloved))
                tvState_HelpDetail.text = getString(R.string.unsloved)
            }
            1-> {
                tvState_HelpDetail.setTextColor(resources.getColor(R.color.help_solved))
                tvState_HelpDetail.text = getString(R.string.solved)
            }
            else -> {
                tvState_HelpDetail.text = getString(R.string.unknown)
            }
        }

        //加载系统默认头像
        setNameAvatar(data?.username?:"无" , ivAvatar_HelpDetail)

        
    }
}